package com.weixinli.web.actions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.imageio.stream.FileImageInputStream;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.weixinli.beans.FmArticle;
import com.weixinli.beans.PageInfo;
import com.weixinli.beans.UserComment;
import com.weixinli.beans.UserInfo;
import com.weixinli.biz.IFmArticleBiz;
import com.weixinli.biz.IUserCommentBiz;
import com.weixinli.biz.IUserGetBiz;
import com.weixinli.biz.IUserGoodBiz;
import com.weixinli.biz.impl.UserGetBizImpl;
import com.weixinli.utils.PageUtil;
import com.weixinli.utils.SessionAttributeName;
import com.weixinli.web.actions.model.JsonObject;
@Controller("FmArticleAction")
public class FmArticleAction extends ActionSupport implements ModelDriven<FmArticle>,SessionAware {
	private static final long serialVersionUID = 1L;
	
	private JsonObject jsonObject;
	
	private FmArticle fmArticle;
	
	private UserComment userComment;
	
	private IFmArticleBiz ifmArticleBiz;
	
	private Map<String, Object> session;

	private IUserGoodBiz userGoodBiz;
	
	private IUserGetBiz iUserGetBiz;
	
	private IUserCommentBiz iUserCommentBiz;
	
	private String searchContent;
	
	/**
	 * 首页点击音频处理
	 * @return
	 */
	public String fmData(){
		jsonObject=new JsonObject();
		System.out.println(fmArticle.getFid()+"fid==============");
		FmArticle fmArticles=(FmArticle) ifmArticleBiz.change(fmArticle);
		System.out.println(fmArticles+"fmArticles信息");
		session.put(SessionAttributeName.FINDONE,fmArticles);  //将信息存起来
		jsonObject.setResult(1);
		getFmInfo();
		return "success";
	}
	
	/**
	 * 搜索
	 * @return
	 */
	public String search(){
		jsonObject=new JsonObject();
		List<FmArticle> fmArticle1=ifmArticleBiz.findArticle(searchContent);
		if(fmArticle1.size()>0){
			jsonObject.setResult(1);
		}else{
			jsonObject.setResult(0);
		}
		jsonObject.setRows(fmArticle1);
		return "success";
	}
	/**
	 * 显示第一个
	 * @return
	 */
	public String getDatesArticle(){
		jsonObject=new JsonObject();
		FmArticle fmArticle=ifmArticleBiz.getDatesArticle();
		session.put(SessionAttributeName.FINDONE,fmArticle);  //将信息存起来
		getFmInfo();
		jsonObject.setObj(fmArticle);
		return "success";
	}
	/**
	 * 查看最新的信息
	 * @return
	 */
	public String fmMentalityData(){
		jsonObject=new JsonObject();
		List<FmArticle> fmArticle=ifmArticleBiz.Newest();
		//session.put(SessionAttributeName.FMMENTALITYDATA, fmArticle);
		jsonObject.setObj(fmArticle);
		return "success";
	}
	/**
	 * 用户点击切换
	 * @return
	 */
	public String change(){
		jsonObject=new JsonObject();
		FmArticle fmArticles=(FmArticle) ifmArticleBiz.change(fmArticle);
		session.put(SessionAttributeName.FINDONE,fmArticles);  //将信息存起来
		jsonObject.setResult(1);
		return "success";
	}
	/**
	 * 对于用户查询一个类型时的处理
	 * @return
	 */
	public String typeArticle(){
		jsonObject=new JsonObject();
		List<FmArticle> fmArticles=ifmArticleBiz.typeArticle(fmArticle);
		if("烦躁".equals(fmArticle.getFtype())){
			//session.put(SessionAttributeName.FINDWHINY,fmArticles);
		}else if("孤独".equals(fmArticle.getFtype())){
			//session.put(SessionAttributeName.FINDLONE,fmArticles);
		}else if("悲伤".equals(fmArticle.getFtype())){
			//session.put(SessionAttributeName.FINDSAD,fmArticles);
		}else if("快乐".equals(fmArticle.getFtype())){
			//System.out.println(fmArticles+"=======d==========");
			//session.put(SessionAttributeName.FINDHAPPY,fmArticles);
		}else if("迷忙".equals(fmArticle.getFtype())){
			//session.put(SessionAttributeName.FINDLOST,fmArticles);
		}else if("感动".equals(fmArticle.getFtype())){
			//session.put(SessionAttributeName.FINDMOVE,fmArticles);
		}
		jsonObject.setRows(fmArticles);
		fmArticle=null;   //每次使用都需要为null
		return "success";
	}
	
	/**
	 * 用户点赞
	 * @return
	 */
	public String fmArticleGood(){
		jsonObject=new JsonObject();
		UserInfo uf=(UserInfo) session.get(SessionAttributeName.CURRENTUSER);
		if(uf==null){
			jsonObject.setResult(1);
		}else{
			String gid=userGoodBiz.findUserGood(uf.getW_uid(), 1);
			String fid=String.valueOf(fmArticle.getFid());
			if(gid==null){
				userGoodBiz.addGood(uf.getW_uid(),fid,1);
			}else{
				if(gid.indexOf(",")>0){//判断是否有,号
					gid=gid+","+fid;
					String[] temp=gid.split(",");
					String str="";//中间一个字符串
					int count=0;//记数
					for(int i=0;i<temp.length;i++){
						if(temp[i].equals(fid)){
							count++;
						}
						str+=temp[i]+",";
					}
					if(count==2){//消除
						fid=str.replaceAll(fid+",","");
						fid=fid.substring(0, fid.lastIndexOf(","));
					}else if(count==1){//添加
						fid=str.substring(0, str.lastIndexOf(","));
					}
					userGoodBiz.updateGood(uf.getW_uid(), fid, 1);
				}else if(!fid.equals(gid)){//第二条数据添加
					fid=gid+","+fid;
					userGoodBiz.updateGood(uf.getW_uid(), fid,1); 
				}else{
					userGoodBiz.delUserGood(uf.getW_uid(), 1);
				}
			}
			jsonObject.setResult(2);
		}
		return "success";
	}
	
	/**
	 * 查询当前显示的页面用户是已点赞  与已经 收藏的
	 * @return
	 */
	public String searchGood(){
		jsonObject=new JsonObject();
		UserInfo uf=(UserInfo) session.get(SessionAttributeName.CURRENTUSER);
		if(uf!=null){
			String fmUserGood=userGoodBiz.findUserGood(uf.getW_uid(), 1);
			if(fmUserGood!=null&&!"".equals(fmUserGood)){
				String[] gid=fmUserGood.split(",");
				for(int i=0;i<gid.length;){
					if(!gid[i].equals(String.valueOf(fmArticle.getFid()))){
						i++;
					}else{
						jsonObject.setResult(2);  //存在
						break;
					}
				}			
			}else{
				jsonObject.setResult(1);  //不存在
			}
		}else{
			jsonObject.setResult(1);  //不存在
		}
		
		if(uf!=null){
			String fmUserGood=iUserGetBiz.findUserGet(uf.getW_uid(), 1);
			if(fmUserGood!=null&&!"".equals(fmUserGood)){
				String[] gid=fmUserGood.split(",");
				for(int i=0;i<gid.length;){
					if(!gid[i].equals(String.valueOf(fmArticle.getFid()))){
						i++;
					}else{
						jsonObject.setTotal(2);;  //存在
						break;
					}
				}			
			}else{
				jsonObject.setTotal(1);  //不存在
			}
		}else{
			jsonObject.setTotal(1);  //不存在
		}
		
		return "success";
	}
	
	/**
	 * 用户收藏
	 * @return
	 */
	public String fmArticleGet(){
		jsonObject=new JsonObject();
		UserInfo uf=(UserInfo) session.get(SessionAttributeName.CURRENTUSER);
		if(uf==null){
			jsonObject.setResult(1);
		}else{
			String gid=iUserGetBiz.findUserGet(uf.getW_uid(), 1);
			String fid=String.valueOf(fmArticle.getFid());
			if(gid==null){
				iUserGetBiz.addGet(uf.getW_uid(),fid,1);
			}else{
				if(gid.indexOf(",")>0){//判断是否有,号
					gid=gid+","+fid;
					String[] temp=gid.split(",");
					String str="";//中间一个字符串
					int count=0;//记数
					for(int i=0;i<temp.length;i++){
						if(temp[i].equals(fid)){
							count++;
						}
						str+=temp[i]+",";
					}
					if(count==2){//消除
						fid=str.replaceAll(fid+",","");
						fid=fid.substring(0, fid.lastIndexOf(","));
					}else if(count==1){//添加
						fid=str.substring(0, str.lastIndexOf(","));
					}
					iUserGetBiz.updateGet(uf.getW_uid(), fid, 1);
				}else if(!fid.equals(gid)){//第二条数据添加
					fid=gid+","+fid;
					iUserGetBiz.updateGet(uf.getW_uid(), fid,1); 
				}else{
					iUserGetBiz.delUserGet(uf.getW_uid(), 1);
				}
			}
			jsonObject.setResult(2);
		}
		return "success";
		
		
	}
	/**
	 * 查询对应某个文章的评论
	 * @return
	 */
	public String findComment(){
		jsonObject=new JsonObject();
		List<UserComment> comment=iUserCommentBiz.FMgetAllComment(1, userComment.getObjid());
		jsonObject.setRows(comment);
		return "success";
	}
	
	/**
	 * 对于用户评论
	 * @return
	 */
	public String findContent(){
		jsonObject=new JsonObject();
		UserInfo uf=(UserInfo) session.get(SessionAttributeName.CURRENTUSER);
		if(uf==null){
			jsonObject.setResult(1);
		}else{
			userComment.setArea_id(1);
			SimpleDateFormat fm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			userComment.setCtime(fm.format(new Date()));
			userComment.setW_uid(uf.getW_uid());
			userComment.setCid(0);
			int data=iUserCommentBiz.addUserComment(userComment);
			if(data>0){
				List<UserComment> comment=iUserCommentBiz.FMgetAllComment(1, userComment.getObjid());
				jsonObject.setRows(comment);
				jsonObject.setResult(0);
			}
		}
		userComment=null;
		return "success";
	}
	
	public String AllNote(){
		jsonObject=new JsonObject();
		List<UserComment> comment=iUserCommentBiz.AllNote(0,10);
		jsonObject.setRows(comment);
		jsonObject.setResult(0);
		return "success";
	}
	
	public String addNote(){
		jsonObject=new JsonObject();
		UserInfo uf=(UserInfo) session.get(SessionAttributeName.CURRENTUSER);
		if(uf==null){
			jsonObject.setResult(1);
		}else{
			SimpleDateFormat fm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			userComment.setCtime(fm.format(new Date()));
			userComment.setW_uid(uf.getW_uid());
			userComment.setArea_id(6);
			int data=iUserCommentBiz.addUserNote(userComment);
			if(data>0){
				List<UserComment> comment=iUserCommentBiz.AllNote(0,10);
				jsonObject.setRows(comment);
				jsonObject.setResult(0);
			}
		}
		userComment=null;
		return "success";
	}
	
	private void getFmInfo(){
		List<FmArticle> fmArticle1=ifmArticleBiz.typeArticle(new FmArticle("烦躁"));
		session.put(SessionAttributeName.FINDWHINY,fmArticle1);
		List<FmArticle> fmArticle2=ifmArticleBiz.typeArticle(new FmArticle("孤独"));
		session.put(SessionAttributeName.FINDLONE,fmArticle2);
		List<FmArticle> fmArticle3=ifmArticleBiz.typeArticle(new FmArticle("悲伤"));
		session.put(SessionAttributeName.FINDSAD,fmArticle3);
		List<FmArticle> fmArticle4=ifmArticleBiz.typeArticle(new FmArticle("快乐"));
		session.put(SessionAttributeName.FINDHAPPY,fmArticle4);
		List<FmArticle> fmArticle5=ifmArticleBiz.typeArticle(new FmArticle("迷忙"));
		session.put(SessionAttributeName.FINDLOST,fmArticle5);
		List<FmArticle> fmArticle6=ifmArticleBiz.typeArticle(new FmArticle("感动"));
		session.put(SessionAttributeName.FINDMOVE,fmArticle6);
	}
	
	public FmArticle getModel() {
		fmArticle=new FmArticle();
		return fmArticle;
	}
	@Resource(name="ifmArticleBiz")
	public void setiFmArticleBiz(IFmArticleBiz iFmArticleBiz) {
		this.ifmArticleBiz = iFmArticleBiz;
	}
	
	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	public JsonObject getJsonObject() {
		return jsonObject;
	}
	public void setSession(Map<String, Object> session) {
		this.session=session;
	}
	@Resource(name="iUserGoodBiz")
	public void setUserGoodBiz(IUserGoodBiz userGoodBiz) {
		this.userGoodBiz = userGoodBiz;
	}
	@Resource(name="iUserGetBiz")
	public void setiUserGetBiz(IUserGetBiz iUserGetBiz) {
		this.iUserGetBiz = iUserGetBiz;
	}
	@Resource(name="iUserCommentBiz")
	public void setiUserCommentBiz(IUserCommentBiz iUserCommentBiz) {
		this.iUserCommentBiz = iUserCommentBiz;
	}
	public void setUserComment(UserComment userComment) {
		this.userComment = userComment;
	}
	public UserComment getUserComment() {
		return userComment;
	}
	public void setSearchContent(String searchContent) {
		this.searchContent = searchContent;
	}
}
